<?php
class ARequerimientoProduccion {

	public function __construct() {
	}

	public function asociarSolicitud(EFacade & $facade) {
		$session = & $facade->getSession();
		$user = NULL;
		$docRP = & $facade->getDocumentRoot();
		$total=0;
		$session->evictAll('MWorkPlace');
		$session->evictAll('MWorkTrans');
		$session->evictAll('DSolicitudProduccionCotizacion');
		$session->evictAll('MTransSet');
		$session->evictAll('MWorkflow');
		foreach ($docRP->asociadoACotizacion as $solicitud){
			$total=$total + $solicitud->epGet('cantidad');
		}
		
		foreach ($docRP->solicitudUsuario as $solicitud){
			$total=$total + $solicitud->epGet('cantidad');
		}
		/////////////////////////////////
		$ezpdo_=getManager();	
		/********************* generar OP *********************/
		$session = & $facade->getSession();
		$user = NULL;
		$docRP = & $facade->getDocumentRoot();
		$solicitudes = & $docRP->asociadoACotizacion;
		$role=NULL;
		$total=0;
		foreach ($docRP->asociadoACotizacion as $solicitud){
			$total=$total + $solicitud->epGet('cantidad');
		}
		
		foreach ($docRP->solicitudUsuario as $solicitud){
			$total=$total + $solicitud->epGet('cantidad');
		}
		$docRP->cantidad=$total;
		$orderProd = $session->create('DOrdenProduccion');
		$role=NULL;
		$orderProd->requerimientoOriginal=$docRP;
		$newEfacade1 =& EFacade :: initWorkflow($session, $orderProd, & $role);
		$newEfacade1->saveWorkflow();
		$transSetOP = & EFacade :: getCurrentTransSetAuto($newEfacade1->getWorkflow(), /* MTrans :: $TYPE_AUTO_PROCESS */ 3);
		$transOP = & $transSetOP->getTransByName('t1');
		$user=NULL;
		$newEfacade1->fireTrans($transOP, $user, "Crear",TRUE);
		$orderProd->epSetDirty(TRUE);
		$newEfacade1->saveWorkflow();
		$orderProd->commit();
		$orderProd->diferenciador = $orderProd->getDocumentDifer();
		$orderProd->numero = $orderProd->getDocumentNumber();
		$orderProd->commit();
		
		/***************** Disparar el transicion ******************/
		$workflowRP = & $docRP->getWorkflowRef();
		$transSetRP = & EFacade :: getCurrentTransSetAuto($workflowRP, /* AGENT */	2);
		$ezpdo_=getManager();
		$transRP = & $transSetRP->getTransByName('t2');
		$facade->fireTrans($transRP, $user, 'Creación de Orden', FALSE);
		$facade->saveWorkflow();
		/******************************************************/
	}
	
	public function actualizarSolicitud(EFacade & $facade) {
		$session = & $facade->getSession();
		$user = NULL;
		$docRP = & $facade->getDocumentRoot();
		echo '<hr>entro al metodo ACTUALIZAR SOLICITUD';
		$solicitudes = & $docRP->asociadoACotizacion;
		$role=NULL;
		foreach ($solicitudes as $solicitud){
			//echo "<hr><hr>la solicitud es: ",$solicitud;
			$newEfacade1 =& EFacade :: initWorkflowWithoutRole($facade->session,$solicitud,MTrans:: $TYPE_AUTO_PROCESS);
			$placeSetSol = & EFacade :: getCurrentPlaceSet($newEfacade1->getWorkflow());
			$wfSOL=$newEfacade1->getWorkflow();
			//echo "<br>el workflow de la solicitud es: ",$newEfacade1->getWorkflow();
			//echo "<hr><hr>el place Set es: ",$placeSetSol;
			
			$transSetSOL = & EFacade :: getCurrentTransSetAuto($newEfacade1->getWorkflow(), /* MTrans :: $TYPE_AUTO_PROCESS */ 3);
			//echo "el transsetSol es: ", $transSetSOL;
			$transSOL = & $transSetSOL->getTransByName('t5');
			//echo "<br><br>antes del fire trans de transSol. TransSet es: ",$transSetSOL;
			//foreach ($transSetSOL->transList as $t)
			//	echo "<hr> el transSol es: ",$t;
			//echo "<br><br>antes del fire trans de transSol.Trans es: ",$transSOL;
			$newEfacade1->fireTrans($transSOL, $user, "Crear",TRUE);
			//echo "<br>despues del fireTrans de sol<br>";
			$solicitud->epSetDirty(TRUE);// marcar el documento para que sea salvado los cambios en su lista de items
			$wfSOL->epSetDirty(TRUE);
			//echo "coloca dirty";
			$wfSOL->commit();
			$newEfacade1->saveWorkflow();
			$placeSetSol = & EFacade :: getCurrentPlaceSet($newEfacade1->getWorkflow());
			//echo "<br><br>luego del firetrans y commit el placeSet es: ",$placeSetSol;
			if($placeSetSol->epGet('name')=='ps4'){
				throw new Exception('<hr><hr><hr><hr><hr><hr>placeSet no cambia');
			}
			unset($newEfacade1);
			$session->evictAll('MPlaceSet');
			$session->evictAll('MPlaceState');
		}
		$workflowRP = & $docRP->getWorkflowRef();
		$facade->session->refresh($workflowRP);
		$transSetRP = & EFacade :: getCurrentTransSetAuto($workflowRP, /* MTrans :: $TYPE_AUTO_AGENT */	2);
		$transRP = & $transSetRP->getTransByName('t5');	
		$facade->fireTrans($transRP, $user, 'Planificación de solicitudes', FALSE);
		$workflowRP->epSetDirty(TRUE);
		$docRP->epSetDirty(TRUE);
		$facade->saveWorkflow();	
		
		
	}

}
?>